#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TraineeProvider : CY.GFive.Core.Providers.DALProvider.ITraineeProvider
	{
		#region Sql
		private static readonly string SqlSelectTrainee = "SELECT [Id], [Code],[Name],[Gender],[Birthday],[Photo],[IdentifyCate],[IdentifyCode],[Phone],[Address],[TrainingCode],[PoliticalCode] FROM [Trainee] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTrainee = "INSERT INTO [Trainee]([Code],[Name],[Gender],[Birthday],[Photo],[IdentifyCate],[IdentifyCode],[Phone],[Address],[TrainingCode],[PoliticalCode]) values(@Code,@Name,@Gender,@Birthday,@Photo,@IdentifyCate,@IdentifyCode,@Phone,@Address,@TrainingCode,@PoliticalCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTrainee = "UPDATE [Trainee] SET [Code] = @Code,[Name] = @Name,[Gender] = @Gender,[Birthday] = @Birthday,[Photo] = @Photo,[IdentifyCate] = @IdentifyCate,[IdentifyCode] = @IdentifyCode,[Phone] = @Phone,[Address] = @Address,[TrainingCode] = @TrainingCode,[PoliticalCode] = @PoliticalCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTrainee = "DELETE [Trainee] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTrainee = "SELECT [Id], [Code],[Name],[Gender],[Birthday],[Photo],[IdentifyCate],[IdentifyCode],[Phone],[Address],[TrainingCode],[PoliticalCode] FROM [Trainee]";
		#endregion

		#region ITraineeProvider Members
		public Core.Business.Trainee Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTrainee);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Trainee trainee = new Core.Business.Trainee();

				if (!reader.IsDBNull(0)) trainee.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) trainee.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) trainee.Name = reader.GetString(2);
				if (!reader.IsDBNull(3)) trainee.Gender = reader.GetByte(3);
				if (!reader.IsDBNull(4)) trainee.Birthday = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) trainee.Photo = reader.GetString(5);
				if (!reader.IsDBNull(6)) trainee.IdentifyCate = reader.GetString(6);
				if (!reader.IsDBNull(7)) trainee.IdentifyCode = reader.GetString(7);
				if (!reader.IsDBNull(8)) trainee.Phone = reader.GetString(8);
				if (!reader.IsDBNull(9)) trainee.Address = reader.GetString(9);
				if (!reader.IsDBNull(10)) trainee.TrainingCode = reader.GetString(10);
				if (!reader.IsDBNull(11)) trainee.PoliticalCode = reader.GetString(11);

				return trainee;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Trainee trainee)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, trainee.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, trainee.Name);
			sql.AddParameter("@Gender", SqlDbType.TinyInt, trainee.Gender);
			sql.AddParameter("@Birthday", SqlDbType.DateTime, trainee.Birthday);
			sql.AddParameter("@Photo", SqlDbType.NVarChar, trainee.Photo);
			sql.AddParameter("@IdentifyCate", SqlDbType.NVarChar, trainee.IdentifyCate);
			sql.AddParameter("@IdentifyCode", SqlDbType.NVarChar, trainee.IdentifyCode);
			sql.AddParameter("@Phone", SqlDbType.NVarChar, trainee.Phone);
			sql.AddParameter("@Address", SqlDbType.NVarChar, trainee.Address);
			sql.AddParameter("@TrainingCode", SqlDbType.NVarChar, trainee.TrainingCode);
			sql.AddParameter("@PoliticalCode", SqlDbType.NVarChar, trainee.PoliticalCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTrainee);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) trainee.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Trainee trainee)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, trainee.Code);
			sql.AddParameter("@Name", SqlDbType.NVarChar, trainee.Name);
			sql.AddParameter("@Gender", SqlDbType.TinyInt, trainee.Gender);
			sql.AddParameter("@Birthday", SqlDbType.DateTime, trainee.Birthday);
			sql.AddParameter("@Photo", SqlDbType.NVarChar, trainee.Photo);
			sql.AddParameter("@IdentifyCate", SqlDbType.NVarChar, trainee.IdentifyCate);
			sql.AddParameter("@IdentifyCode", SqlDbType.NVarChar, trainee.IdentifyCode);
			sql.AddParameter("@Phone", SqlDbType.NVarChar, trainee.Phone);
			sql.AddParameter("@Address", SqlDbType.NVarChar, trainee.Address);
			sql.AddParameter("@TrainingCode", SqlDbType.NVarChar, trainee.TrainingCode);
			sql.AddParameter("@PoliticalCode", SqlDbType.NVarChar, trainee.PoliticalCode);
			sql.AddParameter("@Id", SqlDbType.Int, trainee.Id);
			sql.ExecuteSql(SqlUpdateTrainee);
		}

		public void Delete(Core.Business.Trainee trainee)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, trainee.Id);
			sql.ExecuteSql(SqlDeleteTrainee);
		}

		public IList<Core.Business.Trainee> GetAllTrainee()
		{
			IList<Core.Business.Trainee> traineelist = new List<Core.Business.Trainee>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTrainee);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Trainee trainee = new Core.Business.Trainee();

					if (!reader.IsDBNull(0)) trainee.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) trainee.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) trainee.Name = reader.GetString(2);
					if (!reader.IsDBNull(3)) trainee.Gender = reader.GetByte(3);
					if (!reader.IsDBNull(4)) trainee.Birthday = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) trainee.Photo = reader.GetString(5);
					if (!reader.IsDBNull(6)) trainee.IdentifyCate = reader.GetString(6);
					if (!reader.IsDBNull(7)) trainee.IdentifyCode = reader.GetString(7);
					if (!reader.IsDBNull(8)) trainee.Phone = reader.GetString(8);
					if (!reader.IsDBNull(9)) trainee.Address = reader.GetString(9);
					if (!reader.IsDBNull(10)) trainee.TrainingCode = reader.GetString(10);
					if (!reader.IsDBNull(11)) trainee.PoliticalCode = reader.GetString(11);

					trainee.MarkOld();
					traineelist.Add(trainee);
				}
				reader.Close();
			}
			return traineelist;
		}
		#endregion
	}
}
